package org.enlongWu.config;

import org.apache.catalina.connector.Connector;
import org.enlongWu.interceptors.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //登录、注册、邮箱发送、消息发送、权限集获取、消息类型集获取、申请状态集获取、冷库状态操作集获取 接口不拦截
        registry.addInterceptor(loginInterceptor).excludePathPatterns("/user/login","/user/register","/send","/role/list","/message/send","/message/typeList","/message/application/stateList", "/coldStorage/state/list");

    }

    /**
     * 想要解决报错
     * java.lang.IllegalArgumentException: Invalid character found in the request target [/user/updateAvatar?avatarUrl=D:\Users\Lenovo\Desktop\ly.png ]. The valid characters are defined in RFC 7230 and RFC 3986
     * 但好像并没生效，那就在Javascript中编码解决吧
     */
    @Bean
    public TomcatServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.addConnectorCustomizers((Connector connector) -> {
            connector.setProperty("relaxedPathChars", "\"<>[\\]^`{|}");
            connector.setProperty("relaxedQueryChars", "\"<>[\\]^`{|}");
        });
        return factory;
    }
}
